//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#if _MSC_VER > 1000
#pragma once
#endif

#ifndef __CRWLOCK_H_
#define __CRWLOCK_H_

#include <elasys_server.h>
#include <interlocked.h>

class CReaderWriterLock : public IReaderWriterLock
{
// Interface functions
//
public:
    CARAPI_(PInterface) Probe(
        /* [in]  */ REIID riid);

    CARAPI_(UInt32)AddRef();

    CARAPI_(UInt32)Release();

    CARAPI Aggregate(
        /* [in]  */ AggregateType type,
        /* [in]  */ PInterface pObject);

    CARAPI GetDomain(
        /* [out] */ PInterface *ppObject);

    CARAPI ReaderLock(
        /* [out] */ WaitResult *pResult);

    CARAPI ReaderTryLock(
        /* [in] */ Millisecond32 msTimeout,
        /* [out] */ WaitResult *pResult);

    CARAPI ReaderUnlock();

    CARAPI UpgradeToWriter(
        /* [out] */ WaitResult *pResult);

    CARAPI TryUpgradeToWriter(
        /* [in] */ Millisecond32 msTimeout,
        /* [out] */ WaitResult *pResult);

    CARAPI WriterLock(
        /* [out] */ WaitResult *pResult);

    CARAPI WriterTryLock(
        /* [in] */ Millisecond32 msTimeout,
        /* [out] */ WaitResult *pResult);

    CARAPI WriterUnlock();

    CARAPI DowngradeFromWriter();

    CARAPI IsReaderLockHeld(
        /* [out] */ Boolean *pIsHeld);

    CARAPI IsWriterLockHeld(
        /* [out] */ Boolean *pIsHeld);

private:
    //todo;
        
private:
    Interlocked m_Refs;    
};

#endif //!(__CRWLOCK_H_)
